package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.dto.LoginDTO;
import com.example.springboot.controller.request.AdminLoginRequest;
import com.example.springboot.controller.request.AdminPageRequest;
import com.example.springboot.entity.Admin;
import com.example.springboot.service.IAdminService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    IAdminService adminService;

    @PostMapping("/login")
    public Result login(@RequestBody AdminLoginRequest adminLoginRequest){
        LoginDTO dto = adminService.login(adminLoginRequest);
        return Result.success(dto);
    }


    @GetMapping("/list")
    public Result list(){
        List<Admin> admin = adminService.list();
        return Result.success(admin);
    }

    @GetMapping("/page")
    public Result page(AdminPageRequest adminPageRequest){
        PageInfo<Admin> page = adminService.page(adminPageRequest);
        return Result.success(page);
    }

    @PostMapping("/save")
    public Result save(@RequestBody Admin admin){
        adminService.save(admin);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Admin admins = adminService.getById(id);
        return Result.success(admins);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Admin admin){
        adminService.update(admin);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delById(@PathVariable Integer id){
        adminService.delById(id);
        return Result.success();
    }

    @GetMapping("/isLegal")
    public Result isLegal(){
        return Result.success();
    }

}
